﻿using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;

namespace Ycjw.PipeLine.Commons.SerializeHelper
{
    public class IniHelper
    {
        private static readonly string DEFAULT_SECTION = "DEFAULT";
        private static readonly string LOOKAT_SECTION = "LOOKAT";
        public static readonly string KEY_LAST_WORKSPACE_PATH = "LastWorkSpacePath";
        private static readonly string KEY_DIRECTION_SYMBLEID = "DirectionSymbleID";
        private static string iniCfgPath
        {
            get
            {
                return Paths.ApplicationConfigDir + "/" + "default.ini";
            }
        }
        public static void WriteLookAt(string[] keys,object[] values)
        {
            for(var i = 0;i < keys.Length;i++)
            {
                WritePrivateProfileString(LOOKAT_SECTION, keys[i], values[i].ToString(), iniCfgPath);
            }
        }
        public static Dictionary<string,string> ReadLookAt(string[] keys)
        {
            var values = new Dictionary<string, string>();
            for (var i = 0;i < keys.Length; i++)
            {
                var val = new StringBuilder();
                GetPrivateProfileString(LOOKAT_SECTION, keys[i], string.Empty, val, 1024, iniCfgPath);
                values.Add(keys[i], val.ToString());
            }
            return values;
        }
        public static string Read(string key, string defaultVal = "")
        {
            var val = new StringBuilder();
            GetPrivateProfileString(DEFAULT_SECTION, key, defaultVal, val, 1024, iniCfgPath);
            return val.ToString();
        }

        public static string DirectionSymbleID
        {
            get
            {
                var val = new StringBuilder();
                GetPrivateProfileString(DEFAULT_SECTION, KEY_DIRECTION_SYMBLEID, "54420004", val, 1024, iniCfgPath);
                return val.ToString();
            }
        }

        public static void Write(string key, string val)
        {
            WritePrivateProfileString(DEFAULT_SECTION, key, val, iniCfgPath);
        }
        /// <summary>
        /// 写入INI文件
        /// </summary>
        /// <param name="section">节点名称[如[TypeName]]</param>
        /// <param name="key">键</param>
        /// <param name="val">值</param>
        /// <param name="filepath">文件路径</param>
        /// <returns></returns>
        [DllImport("kernel32")]
        public static extern bool WritePrivateProfileString(string section, string key, string val, string filepath);

        /// <summary>
        /// 读取INI文件
        /// </summary>
        /// <param name="section">节点名称</param>
        /// <param name="key">键</param>
        /// <param name="def">值</param>
        /// <param name="retval">stringbulider对象</param>
        /// <param name="size">字节大小</param>
        /// <param name="filePath">文件路径</param>
        /// <returns></returns>
        [DllImport("kernel32")]
        public static extern bool GetPrivateProfileString(string section, string key, string def, StringBuilder retval, int size, string filePath);
    }
}